<?php

namespace Sharks\Core\LifeCycle;

/**
 * 错误处理
 */
abstract class ErrorResolver {
	private static $resolvers;

	public static function register(ErrorResolver $handler) {
		if (is_null(self::$resolvers)) {
			self::$resolvers = array();
		}
		self::$resolvers[] = $handler;
	}

	public static function handle($response, $exception) {
		if (is_null(self::$resolvers)) {
			return;
		}

		$response->setView(null);
		foreach (self::$resolvers as $resolver) {
			$resolver->resolve($response, $exception);
			if ($response->hasView()) {
				return;
			}
		}
	}

	abstract function resolve($response, $exception);
}